<%-- 
    Document   : transaction
    Created on : Mar 28, 2012, 11:37:54 AM
    Author     : pauld
--%>

<%@page import="Model.Transaction"%>
<%@page import="java.io.UnsupportedEncodingException"%>
<%@page import="java.security.NoSuchAlgorithmException"%>
<%@page import="Controller.UserManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <%
    //get the user and name session variable
    String user = (String)session.getAttribute("user");
    String name = (String)session.getAttribute("name");

    //if the user is logged in check for session integrity
    if( user == null || 
            (UserManager.isAdmin(user) != 0 || 
            UserManager.isFinanceManager(user) != 0 || 
            UserManager.isProductManager(user) != 0) ) {

        //if the user is not logged in or 
        //is a manager/admin redirect to the index page
        response.sendRedirect("index.jsp");

    }
    else if( user != null &&
                (UserManager.isAdmin(user) == 0 && 
                UserManager.isFinanceManager(user) == 0 && 
                UserManager.isProductManager(user) == 0) ){

        //check session integrity by comparing the hash of the session token
        String currSession = request.getRemoteAddr().concat( session.getId() );
        try{
            if( !UserManager.checkSessionIntegrity(currSession, session) ){
                UserManager.logout(request);
                response.sendRedirect("login.jsp");
            }
        }catch(NoSuchAlgorithmException e1){
            UserManager.logout(request);
            response.sendRedirect("login.jsp");
        }
        catch(UnsupportedEncodingException e1){
            UserManager.logout(request);
            response.sendRedirect("login.jsp");
        }
        catch(Exception e1){
            UserManager.logout(request);
            response.sendRedirect("login.jsp");
        }

    }
    
    
    %>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
        <link rel="stylesheet" href="css/960_24_col.css" />
        <link rel="stylesheet" href="css/reset.css" />
        <link rel="stylesheet" href="css/text.css" />
        <link rel="stylesheet" href="css/style.css" />
        
        <script type="text/javascript" src="scripts/jquery.js"></script>
        <script type="text/javascript" src="scripts/formScripts.js"></script>
        
        <title>FooBar Online Bookstore | Buy Product</title>
    </head>
    <body>
        <div id="bar"></div>
        <!-------------------------------header here----------------------->
            <div id="header-wrap">
                <div id="dashboard" class="container_24">
                    <div id="dashboard-logo" class="grid_7">
                        <a href="index.jsp"><img src="images/logo.png" alt="logo" /></a>
                    </div>
                    <div id="dashboard-menu" class="grid_17">
                        <ul class="menu" id="main-menu">
                            <li><a href="index.jsp">Home</a></li>
                            <li><a href="main.jsp">Products</a>
                                <ul style="display: none;">
                                    <li><a href="main.jsp?show=book">Books</a></li>
                                    <li><a href="main.jsp?show=mags">Magazines</a></li>
                                    <li><a href="main.jsp?show=cd">CD</a></li>
                                    <li><a href="main.jsp?show=dvd">DVD</a></li>
                                    <li><a href="main.jsp?show=acc">Accessories</a></li>
                                </ul>
                            </li>
                            <%
                            if( user != null && UserManager.isAdmin(user) == 0 && UserManager.isFinanceManager(user) == 0 && UserManager.isProductManager(user) == 0 ){
                                out.println("<li><a href='myProfile.jsp'>My Profile</a></li>");
                            }
                            else if(UserManager.isAdmin(user) == 1){
                                out.println("<li><a href='admin/adminPanel.jsp'>Admin Panel</a></li>");
                            }
                            else if(UserManager.isFinanceManager(user) == 1){
                                out.println("<li><a href='admin/financePanel.jsp'>Sales Panel</a></li>");
                            }
                            else if(UserManager.isProductManager(user) != 0){
                                out.println("<li><a href='admin/productPanel.jsp'>Product Panel</a></li>");
                            }
                            %>
                            <li><a href="about.jsp">About</a></li>
                            <li><a href="contact.jsp">Contact Us</a></li>
                            <%
                            if(user == null){
                                out.println("<li><a href='login.jsp'>Login</a> | <a href='register.jsp'>Join Now</a></li>");
                            }
                            else{
                                out.println("<li><form action='logout' name='logout' method='post'>"
                                + "<a id='user-header' href='myProfile.jsp'>" + name + "</a>"
                                + "<input type='submit' value='Logout' class='submit-button' />"
                                + "</form></li>");
                            }
                            %>
                        </ul>
                    </div>
                </div>
            </div>
        <!-------------------------------content here----------------------->
        <div id="content-wrap">
            <div id="content" class="container_24">
                <div class="grid_10 prefix_7">
                    <form action="Transaction" class="styled-form" id="buy-form" method="post">
                        <ul class="error-pane">
                            <%
                            try{
                                if( request.getParameter("status").equals("1") ){
                                    out.println("<li class='important success'>Transaction Successful Thank You<br />"
                                            + "<a href='main.jsp'>Back to products page</a></li>");
                                }
                                else if( request.getParameter("status").equals("0") ){
                                    out.println("<li class='important'>Credit card information and password combination is incorrect</li>");
                                }
                                else if( request.getParameter("status").equals("-1") ){
                                    out.println("<li class='important error'>Ooops Something Went Wrong Please Try Again Later</li>");
                                }
                            }
                            catch(NullPointerException e1){}
                            %>
                            <li style="display: none;" id="buy-verification-error">Please enter a valid credit card verification number</li>
                            <li style="display: none;" id="buy-credit-error">Please enter a valid credit card number</li>
                        </ul>
                        <fieldset>
                            <label>Credit Card Number</label><br />
                            <input type="text" name="credit-num" id="buy-credit-num" class="form-inputs" maxlength="16" /><br />
                            <span>We only accept Master and Visa</span><br />
                        </fieldset>
                        <fieldset>
                            <label>Expiration Date</label><br />
                            <select name="expiration-month" id="buy-expire-month">
                                <option value="">---Month---</option>
                                <option value="1">January</option>
                                <option value="2">February</option>
                                <option value="3">March</option>
                                <option value="4">April</option>
                                <option value="5">May</option>
                                <option value="6">June</option>
                                <option value="7">July</option>
                                <option value="8">August</option>
                                <option value="9">September</option>
                                <option value="10">October</option>
                                <option value="11">November</option>
                                <option value="12">December</option>
                            </select>
                            <select name="expiration-year" id="buy-expire-year">
                                <option value="">--Year--</option>
                                <%
                                int y = Transaction.getYear(),
                                        yy = 0,
                                        i = 0;
                                
                                for(i = 0; i < 11; i++){
                                    yy = y+i;
                                    out.println("<option value=" + yy + ">" + yy + "</option>");
                                }
                                %>
                                <!--option value="2012">2012</option>
                                <option value="2013">2013</option>
                                <option value="2014">2014</option>
                                <option value="2015">2015</option>
                                <option value="2016">2016</option>
                                <option value="2017">2017</option>
                                <option value="2018">2018</option>
                                <option value="2019">2019</option>
                                <option value="2020">2020</option-->
                            </select>
                        </fieldset>
                        <fieldset>
                            <label>Card Verification Number</label><br />
                            <input type="text" class="form-inputs" id="buy-verification" name="credit-verification" maxlength="3" /><br />
                            <span>(Last three digits at the back of your credit card)</span><br />
                        </fieldset>
                            
                        <fieldset>
                            <label>Password</label><br />
                            <input type="password" class="form-inputs" name="buy-pw" maxlength="25" />
                        </fieldset>
                        <input type="submit" class="submit-button" value="Continue" />
                    </form>
                </div>
            </div>
        </div>
        <!-------------------------------footer here----------------------->
        <div id="upper-footer-wrap">
            <div id="upper-footer" class="container_24">
                <div class="grid_7 prefix_17">
                    <ul class="menu">
                        <li><a href="#">top</a></li>
                        <li><a href="index.jsp">home</a></li>
                        <li><a href="contact.jsp">contact us</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div id="lower-footer-wrap">
            <div id="lower-footer" class="container_24">
                <center>
                    <p>FooBar Online Bookstore SECPROG Project</p>
                </center>
            </div>
        </div>
    </body>
</html>
